package dubrouski.csc;

/**
 *
 * @author Stanislau Dubrouski
 */
class BasicThresholderImpl implements Thresholder {

    public int[][][] thresholdImage(int[][][] image, int thresholdLevel) {
        int[][][] resultImage = new int[image.length][image[0].length][image[0][0].length];

        for (int z = 0; z < image[0][0].length; z++) {
            for (int x = 0; x < image.length; x++) {
                for (int y = 0; y < image[0].length; y++) {
                    int red = (int) (image[x][y][z] & 0xff0000) >> 16;
                    int green = (int) (image[x][y][z] & 0x00ff00) >> 8;
                    int blue = (int) (image[x][y][z] & 0x0000ff);
                    red = (red < thresholdLevel) ? 0 : red;
                    green = (green < thresholdLevel) ? 0 : green;
                    blue = (blue < thresholdLevel) ? 0 : blue;
                    resultImage[x][y][z] = ((red & 0xff) << 16) + ((green & 0xff) << 8) + (blue & 0xff);
                    //TEST - paint threshold results
                    //pixels[y * xSize + x] = image[x][y][z];
                }
            }
        }

        return resultImage;
    }
    
//BACKUP
//    public int[][][] thresholdImage(int[][][] image, int[] imageSize, int thresholdLevel) {
//        int[][][] resultImage = new int[imageSize[X]][imageSize[Y]][imageSize[Z]];
//
//        for (int z = 0; z < imageSize[Z]; z++) {
//            for (int x = 0; x < imageSize[X]; x++) {
//                for (int y = 0; y < imageSize[Y]; y++) {
//                    int red = (int) (image[x][y][z] & 0xff0000) >> 16;
//                    int green = (int) (image[x][y][z] & 0x00ff00) >> 8;
//                    int blue = (int) (image[x][y][z] & 0x0000ff);
//                    red = (red < thresholdLevel) ? 0 : red;
//                    green = (green < thresholdLevel) ? 0 : green;
//                    blue = (blue < thresholdLevel) ? 0 : blue;
//                    resultImage[x][y][z] = ((red & 0xff) << 16) + ((green & 0xff) << 8) + (blue & 0xff);
//                    //TEST - paint threshold results
//                    //pixels[y * xSize + x] = image[x][y][z];
//                }
//            }
//        }
//
//        return resultImage;
//    }
}